package com.ptu.servlet;

import com.ptu.entity.Auditing;
import com.ptu.entity.Employee;
import com.ptu.service.AuditingService;
import com.ptu.service.ExpenseService;
import com.ptu.service.impl.AuditingServiceImpl;
import com.ptu.service.impl.ExpenseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 *
 */
@WebServlet("/ptu/AuditingServlet")
public class AuditingServlet extends BaseServlet {

    private AuditingService auditService = new AuditingServiceImpl();
    private ExpenseService expenseService = new ExpenseServiceImpl();

    protected void saveAuditing(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");

        // 1. 接受页面中的数据
        int  expId=Integer.parseInt(req.getParameter("expid"));
        Employee emp = (Employee) req.getSession().getAttribute("emp");
        String empId = emp.getEmpId();
        String result = req.getParameter("result");
        String auditDesc = req.getParameter("auditDesc");
        java.sql.Date time = new java.sql.Date(new Date().getTime());

        Auditing auditing = new Auditing( expId, empId,result, auditDesc,time);

        Integer money = Integer.parseInt(req.getParameter("money"));

        // 获得当前用户上级
        Employee emp1 = (Employee) req.getSession().getAttribute("emp");

        // 2. 数据的处理

        AuditingService  auditingService=new AuditingServiceImpl();

        int save = auditingService.addAudit(auditing,money,emp1.getMgrid());

        // 3. 给用户做出响应
        resp.getWriter().print(save);
    }

    protected void showAuditing(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 1. 接收页面中的数据
        int expId = Integer.parseInt(req.getParameter("expId"));

        // 2. 处理数据
        List<Auditing> auditings = auditService.findAuditing(expId);


        // 3. 给用户做出相应
        req.setAttribute("auditings", auditings);
        req.getRequestDispatcher("/auditHistory.jsp").forward(req, resp);
    }

    protected void showMyAudit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Employee emp = (Employee) req.getSession().getAttribute("emp");

        List<Auditing> auditExpense = auditService.findMyAudit(emp.getEmpId());
//        List<Auditing> auditExpense = auditService.findMyAudit("1");
//        Auditing auditing = auditExpense.get(0);
//        System.out.println(auditing);
//        System.out.println(auditing.getExpense());
        req.setAttribute("auditExpense", auditExpense);
        req.getRequestDispatcher("/myAudit.jsp").forward(req, resp);
    }
}
